﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using EverBuy.SocialNetworking.BusinessObjects;
using EverBuy.SocialNetworking.Data.EntityFrameworkRepository.Abstracts;
using EverBuy.SocialNetworking.Data.Repository.Abstract;

namespace EverBuy.SocialNetworking.Data.EntityFrameworkRepository
{
    /// <summary>
    /// 
    /// </summary>
    public class AccountRepository : RepositoryBase, IAccountRepository
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountID"></param>
        /// <returns></returns>
        public BusinessObjects.Account GetAccountByID(int accountID)
        {
            try
            {
                var account = (from a in DataContext.Accounts
                               where a.AccountID == accountID
                               select a).FirstOrDefault();
                return ModelMapper.Mapper.Map(account);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public BusinessObjects.Account GetAccountByEmail(string email)
        {
            try
            {
                var account = (from a in DataContext.Accounts
                               where a.Email == email
                               select a).FirstOrDefault();
                return ModelMapper.Mapper.Map(account);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>a
        /// <returns></returns>
        public BusinessObjects.Account GetAccountByUsername(string username)
        {
            var account = (from a in DataContext.Accounts
                           where a.Username == username
                           select a).FirstOrDefault();
            return ModelMapper.Mapper.Map(account);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="account"></param>
        public void SaveAccount(BusinessObjects.Account account)
        {
            //if (account.ID > 0)
            //    DataContext.Attach(ModelMapper.Mapper.Map(account));
            //else
            //    DataContext.AddObject(ModelMapper.Mapper.Map(account));

            DataContext.SaveChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="account"></param>
        public void DeleteAccount(BusinessObjects.Account account)
        {
            DataContext.DeleteObject(ModelMapper.Mapper.Map(account));
            DataContext.SaveChanges();
        }

        public List<Account> SearchAccounts(string searchText)
        {
            throw new NotImplementedException();
        }
    }
}
